<?php
namespace app\index\controller;

use app\common\enums\ResultEnum;
use app\common\model\Cars;
use app\common\service\SecureService;
use app\common\utils\ResultVOUtil;
use think\Request;

class Secure extends Base{
    protected $secureService;
    
    public function __construct(SecureService $secureService)
    {
        parent::initialize();
        $this->secureService = $secureService;
    }
    
    public function index(Request $request){
        $param = $request->param();
        $where = [];
        if(!empty($param)){
            
            if ( !empty($param['search']) ) {
                $carId = Cars::where('car_sn|car_unique_id','like','%'.$param['search'].'%')->column('car_id');
                $where[] = ['car_id','in',$carId];
            }
            
            if ( !empty($param['brand_id']) ) {
                $carId = Cars::where(['brand_id'=>$param['brand_id']])->column('car_id');
                $where[] = ['car_id','in',$carId];
            }
            
            if ( !empty($param['agency_id']) ) {
                $carId = Cars::where(['agency_id'=>$param['agency_id']])->column('car_id');
                $where[] = ['car_id','in',$carId];
            }
            
            if ( !empty($param['secure_company']) ) {
                $where[] = ['secure_company','in',$param['secure_company']];
            }
            
            if ( !empty($param['secure_type']) ) {
                $where[] = ['secure_type','in',$param['secure_type']];
            }
            
            if ( !empty($param['status']) ) {
                $where[] = ['status','in',$param['status']];
            }
        }
        $result = $this->secureService->index($where, $param['length'], $param['page']);
        
        if($result){
            return ResultVOUtil::success($result);
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
    }
    
    public function save(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->secureService->save($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function list($id=0){
        $data = $this->secureService->list($id);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }
    }
    
    public function edit(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->secureService->edit($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function delFile(String $url=""){
        if(!empty($url)){
            $data = $this->secureService->delFile($url);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::DEL_ERROR);
            }
        }
    }
    
    public function upload(Request $request){
        $param  = $request->param();
        if(!empty($param)){
            $data = $this->secureService->upload($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::UPLOAD_ERROR);
            }
        }
    }
    
    public function del($id=0){
        $data = $this->secureService->del($id);
        if($data){
            return ResultVOUtil::success("");
        }else{
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        }
    }
}